<?php
	include "public/commom/config.php";//引入配置连接数据库文件
	
	if(isset($_SESSION["user_id"])) {//是否登录
		//已登录
		if($_GET) {
			$user_id = $_SESSION["user_id"];//用户id
			$weibo_id = $_GET["weibo_id"];//微博id
			
			$sql = "SELECT * FROM praise WHERE user_id = '$user_id' AND weibo_id = '$weibo_id'";//sql语句 - 查找该用户是否赞过该微博
			
			$result = mysqli_query($conn, $sql);//result - 执行sql语句
			
			if(mysqli_num_rows($result)) {//该用户是否赞过该微博
				//ture - 赞过
				$sql = "DELETE FROM praise WHERE user_id = '$user_id' AND weibo_id = '$weibo_id'";//取消赞
				
				$result = mysqli_query($conn, $sql);//result - 执行sql语句
				
				mysqli_close($conn);//关闭数据库连接
			
				echo "<script>location.href='" . $_SERVER["HTTP_REFERER"] . "';</script>";//返回上一页
			}else {
				//ture - 没赞
				$sql = "INSERT INTO praise (user_id, weibo_id) VALUES ('$user_id', '$weibo_id')";//sql语句  - 插入赞
				
				$result = mysqli_query($conn, $sql);//result - 执行sql语句
				
				mysqli_close($conn);//关闭数据库连接
				
				echo "<script>location.href='" . $_SERVER["HTTP_REFERER"] . "';</script>";//返回上一页
			}
		}
	}else {
		//未登录
		echo "您还未登录,请登录账户。";
	}
?>